#include "dev_lsg_cc.h"

using namespace RFIDLIB;
using namespace std;

CRFID_LSG_CC::CRFID_LSG_CC()
{

}

CRFID_LSG_CC::~CRFID_LSG_CC()
{

}
//LSG Control Center
inline err_t CRFID_LSG_CC::func_LSGCC_SinglelDoor_Alarm(BYTE gate_id)
{
    return LSG_Alaram(hRdr,gate_id);
}
err_t CRFID_LSG_CC::func_LSGCC_SinglelDoor_SetOutput(BYTE gate_id,BYTE output_ids[],BYTE output_len,BYTE type,BYTE freq,DWORD at,DWORD dt){
    RFID_DN_HANDLE dnOutputOper = RDR_CreateSetOutputOperations();
    for (int i = 0; i < output_len; i++)
        RDR_AddOneOutputOperation(dnOutputOper, output_ids[i], type, freq, at, dt);
    int iret = LSG_SetOutput(hRdr,gate_id, dnOutputOper);
    DNODE_Destroy(dnOutputOper);
    return iret;
}
inline err_t CRFID_LSG_CC::func_LSGCC_SinglelDoor_SetAtmosphereLight(BYTE gate_id,BYTE light_id,BYTE type,BYTE freq,DWORD at,DWORD dt){
    return LSG_SetAtmosphereLight(hRdr,gate_id,light_id,type,freq,at,dt);
}
inline err_t CRFID_LSG_CC::func_LSGCC_SinglelDoor_GetIRTriggerCount(BYTE gate_id,DWORD ir_ids[],BYTE* ir_nums){
    return LSG_GetIRTriggerCounter(hRdr,gate_id,ir_ids,ir_nums);
}
inline err_t CRFID_LSG_CC::func_LSGCC_InitPC(BYTE type,DWORD inPC,DWORD outPC){
    return LSG_InitPC(hRdr,type,inPC,outPC);
}
inline err_t CRFID_LSG_CC::func_LSGCC_GetPC(BYTE option, DWORD * pcInTotal, DWORD * pcOutTotal, DWORD * pcInDay, DWORD * pcOutDay){
    return LSG_FetchPCFromGate(hRdr,option,pcInTotal,pcOutTotal,pcInDay,pcOutDay);
}
inline err_t CRFID_LSG_CC::func_LSGCC_GetSystemTime(DWORD* year,BYTE* month,BYTE* day,BYTE* hour,BYTE* minute,BYTE* second){
    return RDR_GetSystemTime(hRdr,year, month, day, hour, minute, second);
}
inline err_t CRFID_LSG_CC::func_LSGCC_SetSystemTime(DWORD year,BYTE month,BYTE day,BYTE hour,BYTE minute,BYTE second){
    return RDR_SetSystemTime(hRdr,year, month, day, hour, minute, second);
}
err_t CRFID_LSG_CC::func_LSGCC_GetRecords(bool* deleteFlag,DWORD Addtionflag,BYTE gate_id,DWORD* total_in,DWORD* total_out,DWORD* day_in,DWORD* day_out){
    //根据标志位获取存储记录和执行报警
    int ret = LSG_FetchStoredDataAndAlaramFromGate(hRdr, *deleteFlag, Addtionflag, gate_id, total_in, total_out, day_in, day_out);
    if (ret != 0)
    {
        *deleteFlag = false;
        return ret;
    }

    *deleteFlag = true;
    if (LSG_GetBufRecordCount(hRdr, buff_type_alarm) > 0)//取到的报警记录个数
    {
        RFID_DN_HANDLE hd = LSG_ReadBufRecord(hRdr, RFID_SEEK_FIRST, buff_type_alarm);//取第一条报警记录
        while (hd != RFID_HANDLE_NULL)
        {
            CRecord_Alarm rcd;
            ret = LSG_ParseAlaramRecord(hd,&rcd.sid, rcd.date, &rcd.eventtype, &rcd.dFlag, rcd.uid, &rcd.uidLen, rcd.user,&rcd.userLen);//解析记录中的UID 和USER区域，对于条码，一般是USER区域
            if (ret == 0)
            {
                rcd.dFlag =(BYTE)(rcd.dFlag & 0x03);
                m_rcd_alarm.push_back(rcd);
            }
            else
                break;
            hd = LSG_ReadBufRecord(hRdr, RFID_SEEK_NEXT, buff_type_alarm);//取下一条报警记录
        }
    }

    if (LSG_GetBufRecordCount(hRdr, buff_type_day) > 0)//取天人流量记录个数
    {
        RFID_DN_HANDLE hd = LSG_ReadBufRecord(hRdr, RFID_SEEK_FIRST, buff_type_day);//取第一条天人流量记录
        while (hd != RFID_HANDLE_NULL)
        {
            CRecord_Day rcd;
            ret = LSG_ParseDayPCRecord(hd, rcd.date, &rcd.inCount, &rcd.outCount);//解析一条天人流量记录中的数据
            if (ret == 0)
                m_rcd_day.push_back(rcd);
            else
                break;
            hd = LSG_ReadBufRecord(hRdr, RFID_SEEK_NEXT, buff_type_day);//取下一条天人流量记录
        }
    }
    if ( LSG_GetBufRecordCount(hRdr, buff_type_realtime) > 0)//
    {
        RFID_DN_HANDLE hd = LSG_ReadBufRecord(hRdr, RFID_SEEK_FIRST, buff_type_realtime);//取当天实时变化记录的第一条
        while (hd != RFID_HANDLE_NULL)
        {
            *deleteFlag = true;
            CRecord_RealTime rcd;
            ret = LSG_ParseRealtimePCRecord(hd, rcd.date, &rcd.direction, &rcd.count);//解析当天实时变化记录
            if (ret == 0)
                m_rcd_realtime.push_back(rcd);
            else
                break;
            hd = LSG_ReadBufRecord(hRdr, RFID_SEEK_NEXT, buff_type_realtime);//取下一条当天实时变化记录
        }
    }
}
